package com.xxxx.mytest.maps

/**
 * @program: day0303
 * @description: 尽信书，则不如无书
 * @author: CoreDao
 * @create: 2021-03-03 15:05
 * */

object Maps {
  def main(args: Array[String]): Unit = {
    //Map合并
    val map = Map("name" -> "zhangsan", "age" -> 19, "money" -> 200)
    val map1 = Map("name" -> "lisi", "age" -> 19, "money" -> 202)
    val map2 = map.++:(map1)
    //注意相同key的覆盖
    map2.foreach(x=>{
      println(x._1+":"+x._2)
    })

    //过滤
    //map对应比较的类型不一致时，编译不通过
    //所以这里只能都是int
    val map3 = Map(("age", 18), ("b", 200))
    map3.filter(x=>{
      if(x._2>20)
        true
      else
        false
    }).foreach(println)

    //count
    println(map3.count(x=>{
      if(x._2>20)
        true
      else
        false
    }))

    //contains
    //让不存在的快速执行
    if (!map3.contains("name")){
      println("no value")
    }else{
      println(map3.get("name"))
    }

    //exists 只要找到符合条件的元素就结束
    //找到一个快速结束
    val bool: Boolean = map3.exists(x => {
      if (x._2 > 2) {
        println(x)
        true
      }
      else
        false
    })





  }


}
